type VitalCapacityMeasurement = {
  value: number;
  unit: string;
  rawData: string;
  timestamp: string;
  isComplete?: boolean; // 添加检测完成标志
};

class VitalCapacityParser {
  static readonly SERVICE_UUID: string = '0000ff30-0000-1000-8000-00805f9b34fb';
  static readonly DATA_CHARACTERISTIC: string = '0000ff31-0000-1000-8000-00805f9b34fb';
  static readonly COMMAND_CHARACTERISTIC: string = '0000ff32-0000-1000-8000-00805f9b34fb';

  // 开始测量命令 (与酒精检测仪相同)
  static readonly START_COMMAND: string = '5533000000000000000000000000017655310000000000000000000000000178';
  // 取消测量命令 (与酒精检测仪相同)
  static readonly STOP_COMMAND: string = '5536000000000000000000000000017355320000000000000000000000000177';

  static parseData(hexString: string): VitalCapacityMeasurement {
    console.log('数据帧:', hexString)
    if (!hexString || hexString.length < 16) { // 至少8字节 = 16个十六进制字符
      console.log('Invalid data: ' + hexString)
    }

    // @ts-ignore
    const bytes = hexString?.match(/.{2}/g).map(b => parseInt(b, 16));
    const rawValue = (bytes[8] << 8) | bytes[7]; // 小端模式组合

    return {
      value: rawValue,
      unit: 'ml',
      rawData: hexString,
      timestamp: new Date().toISOString(),
      isComplete: hexString.charAt(11)==='4'
    };
  }

  static getStartCommand(): string {
    return this.START_COMMAND;
  }

  static getStopCommand(): string {
    return this.STOP_COMMAND;
  }
}

export default VitalCapacityParser;
export type { VitalCapacityMeasurement };

